<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/8
 * Time: 下午4:26
 */
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller{
    public function index(){
        $this -> display();
    }
    public function gologin(){
        $data = ['data' => '', 'msg' => '用户名或密码错误', 'status' => 1];
        if(IS_AJAX){
            $username = I('post.username/s', '');
            $password = I('post.password/s', '', 'strip_tags');

            if(empty($username)){
                $data['msg'] = '用户名不能为空';
                exit(json_encode($data));
            }
            $is_Ok = preg_match('/^[A-Za-z0-9]+$/', $username);
            if(!$is_Ok){
                $data['msg'] = '用户名包含特殊字符';
                exit(json_encode($data));
            }
            if(empty($password)){
                $data['msg'] = '密码不能为空';
                exit(json_encode($data));
            }

//            $admin = M('admin');
//            $adminInfo = $admin -> where(['name' => $username]) -> find();
//
//            if(!$adminInfo){
//                exit(json_encode($data));
//            }
//            if(is_null($adminInfo)){
//                $data['msg'] = $username . '用户不存在';
//                exit(json_encode($data));
//            }
//            if($adminInfo['pwd'] == _md5($password, $adminInfo['salt'])) {
//                $admin -> execute('update dx_admin set loginNum = loginNum + 1 where id ="' . $adminInfo['id'] . '"');
//                session('user', ['uid' => $adminInfo['id'], 'name' => $adminInfo['name'], 'realName' => $adminInfo['realname']]);
//                $data['msg'] = '登录成功';
//                $data['status'] = 0;
//            }

            $admin = D('admin');
            $adminInfo = $admin -> adminInfo($username);
            /**
             * 经测试返回了三维数组  即获取的ID为 $adminInfo['data'][0]['id']
             */
            $info = $adminInfo['data'][0];
            if(1 == $adminInfo['status']){
                exit(json_encode($data));
            }
            if((0 == $adminInfo['status']) && ($info['pwd'] == _md5($password, $info['salt']))) {
                $admin -> updateLoginNum($info['id']);
                $admin -> updateLoginTime($info['id']);
                session('user', ['uid' => $info['id'], 'name' => $info['name'], 'realName' => $info['realname']]);
                $data['msg'] = '登录成功';
                $data['status'] = 0;
            }
        }
        echo json_encode($data);
    }

    public function loginOut(){
        session(null);
        $this -> success('您已成功退出!<br>',U('Admin/login/index'),2);
        exit;
    }

    public function editPwd(){
        $this -> display();
    }
    public function pwdSave(){
        if(IS_AJAX && IS_POST){
            $oldpwd = I('post.oldpwd/s','');
            $newpwd = I('post.newpwd/s','');
            $confirm_pwd= I('post.confirm_pwd/s','');

            if(!$oldpwd){
                $this -> ajaxReturn(['status' => 1, 'msg' => '原密码不能为空']);
            }
            if((!$newpwd) || (!$confirm_pwd)){
                $this -> ajaxReturn(['status' => 1, 'msg' => ' 新密码不能为空']);
            }
            if($newpwd !== $confirm_pwd){
                $this -> ajaxReturn(['status' => 1, 'msg' => '两次输入的密码不一致']);
            }
            $id = session('user.uid');
            $admin = M('Admin');
            $adminInfo = $admin -> where(['id' => $id]) -> find();

            $salt = $adminInfo['salt'];
            $pwd = $adminInfo['pwd'];
            $md5_oldpwd = _md5($oldpwd, $salt);

            if($md5_oldpwd != $pwd){
                $this -> ajaxReturn(['status' => 1, 'msg' => '原密码错误']);
            }
            $md5_newpwd = _md5($newpwd, $salt);
            $isOk = $admin -> where(['id' => $id]) -> setField(['pwd' => $md5_newpwd]);
            if(!$isOk){
                $this -> ajaxReturn(['status' => 1, 'msg' => '修改失败']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '修改成功']);
        }
    }
}